package com.cjq.core.loadblance.impl;

import com.cjq.core.loadblance.LoadBalancer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Component
// 轮询策略实现
public class RoundRobinLoadBalancer implements LoadBalancer {
    private final List<String> instances;
    private final AtomicInteger index = new AtomicInteger(0);

    public RoundRobinLoadBalancer(List<String> instances) {
        this.instances = instances;
    }

    @Override
    public String choose() {
        return instances.get(index.getAndIncrement() % instances.size());
    }
}